package com.mg.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.jfinal.plugin.activerecord.Record;

public class ExcelUtil {
	public static final String HEADERINFO = "headInfo";
	public static final String DATAINFON = "dataInfo";

	public static Workbook getWorkBook(Map<String, Object> map) throws IOException {
		Workbook wb = new XSSFWorkbook(); 
		if (null != map) {
			List<Object> headList = (List<Object>) map.get(ExcelUtil.HEADERINFO);
			List<Record> dataList = (List<Record>) map.get(ExcelUtil.DATAINFON);
			CellStyle style = getCellStyle(wb);
			Sheet sheet = wb.createSheet("sheet1");
			sheet.setColumnWidth(0, 15 * 256);
			sheet.setColumnWidth(1, 20 * 256);
			sheet.setColumnWidth(2, 15 * 256);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
			/**
			 * 设置Excel表的第一行即表头
			 */
			Row row = sheet.createRow(0);
			Cell headCell2 = row.createCell(0);
			headCell2.setCellType(Cell.CELL_TYPE_STRING);
			headCell2.setCellValue("表一");
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			headCell2.setCellStyle(style);// 设置表头样式
			Row row2 = sheet.createRow(1);
			for (int i = 0; i < headList.size(); i++) {
				Cell headCell = row2.createCell(i);
				headCell.setCellType(Cell.CELL_TYPE_STRING);
				headCell.setCellStyle(style);// 设置表头样式
				headCell.setCellValue(String.valueOf(headList.get(i)));
			}

			for (int i = 0; i < dataList.size(); i++) {
				Row rowdata = sheet.createRow(i + 2);// 创建数据行
				Record r = dataList.get(i);
				Object[] vals = r.getColumnValues();
				for (int j = 0; j < vals.length; j++) {
					Cell celldata = rowdata.createCell(j);
					celldata.setCellType(Cell.CELL_TYPE_STRING);
					celldata.setCellValue(vals[j] + "");
					CellStyle cs = wb.createCellStyle();
					cs.setWrapText(true);
					celldata.setCellStyle(cs);
				}
			}
		}
		return wb;
	}

	public static CellStyle getCellStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 12);// 设置字体大小
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
		style.setFillForegroundColor(HSSFColor.LIME.index);// 设置背景色
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND);// 让单元格居中
		// style.setWrapText(true);//设置自动换行
		style.setFont(font);
		return style;
	}
}
